using UnityEngine;

public class UI_PlayerStats : MonoBehaviour
{
    private UI_StatSlot[] statSlots;
    private Inventory_Player inventoryPlayer;

    private void Awake()
    {
        statSlots = GetComponentsInChildren<UI_StatSlot>();
        inventoryPlayer = FindAnyObjectByType<Inventory_Player>();
    }
    private void OnEnable()
    {
        inventoryPlayer.OnInventoryChange += UpdateAllStatSlot;
    }
    private void OnDisable()
    {
        inventoryPlayer.OnInventoryChange -= UpdateAllStatSlot;
    }

    /// <summary>
    /// Unity 不保证子物体Awake一定在父物体OnEnable前执行，顺序依赖引擎内部调度
    /// Unity执行顺序：
    ///     同一物体上，先执行Awake，再执行OnEnable，Start
    ///     父子级关系时：先执行子类的Awake，再执行父类的Awake，    OnEnable，Start同理  //同阶段子物体优先，分阶段批量执行
    /// </summary>
    private void Start()
    {
        UpdateAllStatSlot();
    }
    private void UpdateAllStatSlot()
    {
        foreach (var statSlot in statSlots)
            statSlot.UpdateStatSlotValue();
    }
}
